package cz.muni.fi.pa165.sme.services.converter;

import cz.muni.fi.pa165.sme.api.enumeration.SexTO;
import cz.muni.fi.pa165.sme.api.enumeration.UserRoleTO;
import cz.muni.fi.pa165.sme.api.to.UserTO;
import cz.muni.fi.pa165.sme.dao.entity.User;
import cz.muni.fi.pa165.sme.dao.enumeration.Sex;
import cz.muni.fi.pa165.sme.dao.enumeration.UserRole;
import org.springframework.core.convert.converter.Converter;

/**
 * @author forman
 */
public class UserTO2UserConverter implements Converter<UserTO, User> {

	@Override
	public User convert(final UserTO source) {
		if(source == null) {
			return null;
		}
		User target = new User();
		target.setDateOfBirth(source.getDateOfBirth());
		target.setFirstName(source.getFirstName());
		target.setSecondName(source.getSecondName());
		target.setHeight(source.getHeight());
		target.setId(source.getId());
		target.setSex(convertSex(source.getSexTO()));
		target.setWeight(source.getWeight());
		target.setLogin(source.getLogin());
		target.setPassword(source.getPassword());
		target.setRole(converRole(source.getRoleTO()));

		return target;
	}

	private Sex convertSex(final SexTO sexTO){
		if(sexTO == null) {
			return null;
		}
		return Sex.valueOf(sexTO.name());
	}

	private UserRole converRole(final UserRoleTO roleTO) {
		if(roleTO == null) {
			return null;
		}
		return UserRole.valueOf(roleTO.name());
	}
}
